using Game.Networking.Server;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Voxels;

namespace Game.UI {
	public class ServerDebugWindow : UIWindow {
		public Button SpawnCargoBoxButton;
		public Button SpawnAsteroidButton;
		public Button SpawnShipButton;
		public Button SpawnTurretButton;
		public Button NewWatchedIdSetButton;
		public Button AddIronBlocksButton;
		public Button RespawnAgentButton;

		public TMP_InputField UnitIdInputField;
		public TMP_Text CameraPositionText;

		void Start() {
			SpawnCargoBoxButton.onClick.AddListener(OnSpawnCargoBoxClick);
			SpawnAsteroidButton.onClick.AddListener(OnSpawnAsteroidClick);
			SpawnShipButton.onClick.AddListener(OnSpawnShipClick);
			SpawnTurretButton.onClick.AddListener(OnSpawnTurretClick);
			NewWatchedIdSetButton.onClick.AddListener(SetWatchedId);
			AddIronBlocksButton.onClick.AddListener(AddIronBlocks);
			RespawnAgentButton.onClick.AddListener(RespawnAgent);
		}

		private void Update() {
			var cam = SpectatorCamera.Instance;
			if ( cam ) {
				var pos = cam.transform.position;
				CameraPositionText.text = $"View position: {Mathf.RoundToInt(pos.x)} {Mathf.RoundToInt(pos.y)} {Mathf.RoundToInt(pos.z)}";
			}
		}

		void OnSpawnShipClick() {
			var newUnit = ServerScriptController.Instance.CreateNewUnitAgentPair($"DBG_{Random.Range(0, 99999)}", CommonStaticData.GetRandomTeam());
			SetWatchedId(newUnit.ID);
			Debug.Log($"Created new unit with id {newUnit.ID} and agent entity with id {newUnit.Data.AttachedEntity}");
		}

		void SetWatchedId() {
			if ( uint.TryParse(UnitIdInputField.text, out var id) ) {
				if ( ServerScriptController.Instance.GetUnitById(id) != null ) {
					UIHolder.Instance.HideAllWindows();
					UIHolder.Instance.ShowWindow(WindowType.DebugWindow);
					SetWatchedId(id);
				}				
			}
		}

		void SetWatchedId(uint id) {
			ServerDebugController.Instance.SetWatchedComputingUnitId(id);
		}

		void OnSpawnAsteroidClick() {
			AsteroidGenerator.Instance.GenerateNewAsteroid(Random.insideUnitSphere * 100, 8f);
		}

		void OnSpawnTurretClick() {
			ServerEntityController.Instance.CreateNewEntity(EntityType.SecurityTurret, Random.insideUnitSphere * 30f, Vector3.zero, e => {
				var cast = (SecurityTurret)e;
				if (cast != null) {
					cast.Team = TeamType.Rogue;
				}
			});
		}

		void AddIronBlocks() {
			var watchedId = ServerDebugController.Instance.WatchedUnitId;
			if ( watchedId == 0 ) {
				return;
			}
			var agent = ServerAgentsController.Instance.GetAgentEntity(watchedId);
			if ( agent == null ) {
				return;
			}
			var blocks = new BlockItem() { BlockData = new BlockData(BlockType.IronBlock), Count = 50, ItemType = InventoryItemType.Block };
			agent.Cargo.TryAddItem(blocks);
		}

		void OnSpawnCargoBoxClick() {
			var blockType = AsteroidGenerator.AsteroidBlockSpawnTable[Random.Range(0, AsteroidGenerator.AsteroidBlockSpawnTable.Count)];
			var block = new BlockData(blockType, 0, 255, 0, ColorUtils.ConvertTo565(Color.white), 0);
			var payload = new List<BaseInventoryItem>(1) {
					new BlockItem() {
						BlockData = block,
						Count = Random.Range(1, 10)
					}
				};
			CargoContainer.SpawnFromPayload(Random.insideUnitSphere * 25f, Vector3.zero, payload);
		}

		void RespawnAgent() {
			var watchedId = ServerDebugController.Instance.WatchedUnitId;
			if ( watchedId == 0 ) {
				return;
			}
			var unit = ServerScriptController.Instance.GetUnitById( watchedId );
			if ( unit == null ) {
				return;
			}
			var agent = ServerAgentsController.Instance.GetAgentEntity(watchedId);
			if ( agent != null ) {
				return;
			}

			ServerAgentsController.Instance.SpawnAgent(unit.OwnerUser, watchedId, CommonStaticData.GetRandomTeam());
		}
	}
}
